#include <iostream>
#include <fstream>
#include <ctime>
#include <cmath>
#include "read_signal.hpp"

using namespace std;

// * generate byte stream and save into file
void create_message(const char *file_path ,const int bit_lens)
{
    srand(static_cast<unsigned int>(time(NULL))); // current time as seed
    ofstream opFile(file_path);                   // delfine file object
    if (opFile.is_open())
    { // if file open
        for (int i = 0; i < bit_lens; i++)
        {                                      // write into file
            opFile << floor(rand() % 2 + 0.5); // generate 0 or 1
        }
        opFile.close();                                            // close file
        cout << "bytesteam write into file " << file_path << endl; // console info
    }
    else
    {
        cerr << "Can not open file " << file_path << endl; // console error
    }
}

// * read file
// * input file_path
// * output
// * first:  vecter of bytes
// * second: File size in bytes
// ! remain in the main function destroy the output
std::pair<char *, size_t> read_file(const char *file_path)
{
    ifstream f(file_path, ios::binary); // define file oject
    if (!f.is_open())
    {
        cerr << "Can not open file " << file_path << endl; // console error
        return {nullptr, 0};
    }
    int len;                   // deine length variable
    f.seekg(0, ios::end);      // go to end
    len = f.tellg();           // len = current location
    f.seekg(0, ios::beg);      // return to beginning
    char *buf = new char[len]; // allocate memory for buffer
    f.read(buf, len);          // read whole file into buffer
    f.close();                 // close file
    return {buf, len};         // return file content
}
